<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="节目名称" prop="programName">
        <el-input v-model="dataForm.programName" placeholder="节目名称"></el-input>
      </el-form-item>
      <el-form-item label="分辨率" prop="value">
        <el-select v-model="dataForm.value" placeholder="请选择分辨率" @change="selectGet">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分辨率:" v-if="dataForm.value=='0'">
        <el-input v-model="dataForm.width" placeholder="宽" style="width: 30%" ></el-input>*
        <el-input v-model="dataForm.height" placeholder="高"  style="width: 30%"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        options: [{
          value: '0',
          label: '自定义分辨率'
        }, {
          value: '1',
          label: '1920*1080'
        }, {
          value: '2',
          label: '1280*800'
        }, {
          value: '3',
          label: '1280*752'
        }, {
          value: '4',
          label: '1440*900'
        }, {
          value: '5',
          label: '1024*768'
        }, {
          value: '6',
          label: '1080*1920'
        }],
        dataForm: {
          id: 0,
          programName: '',
          value: '',
          remark: '',
          width:'',
          height:'',
          fenbianlv:''
        },
        dataRule: {
          programName: [
            { required: true, message: '节目名称不能为空', trigger: 'blur' }
          ],
          value: [
            { required: true, message: '请选择分辨率', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      selectGet(){
        let obj = {}
        this.options.map((item) => {
          //  this.unitList 是你 select option遍历的集合 e 是选中的id
          if(item.value === this.dataForm.value) {
            obj = item
          }
        })
        this.dataForm.fenbianlv = obj.label
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if(this.dataForm.value==0){
              if(this.dataForm.width=='' || this.dataForm.height==''){
                this.$message.error('请输入分辨率')
                return
              }
              this.dataForm.fenbianlv = this.dataForm.width+'*'+this.dataForm.height
          }
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/program/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'programName': this.dataForm.programName,
                'fenbianlv': this.dataForm.fenbianlv
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                console.log(data)
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    // this.$emit('refreshDataList')
                    this.$router.push({
                      name: 'programs-edit',
                      params: {
                        status: 'new',
                        id: data.data.id
                      }
                    })
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
